#include <stdarg.h>

#include "slistmethod-default.h"

static void slmdef_destroy_iter(SListIter *iter)
{
	Slmdef *def;

	if(!iter)
		return;

	def=iter->private;
	if(def)
	{
		if(def->icon)
			goo_canvas_item_remove(def->icon);	
		if(def->text)
			goo_canvas_item_remove(def->text);	
		g_free(def);
	}
	if(iter->cavas)
	{
		goo_canvas_item_remove(iter->cavas);
	}

	g_free(iter);
}

static SListIter *slmdef_create_iter(SListView *view, ...)
{
	va_list   ap; 
	SListIter *iter;
	const char *s;
	gboolean err=FALSE;
	GdkPixbuf *pixbuf;
	int b,size;
	Slmdef *def;

	if(!view)
		return NULL;

	iter=(SListIter *)g_malloc0(sizeof(SListIter)+1);
	def=(Slmdef *)g_malloc0(sizeof(Slmdef)+1);
	iter->private=(gpointer)def;
	iter->cavas=goo_canvas_group_new(view->frame,"width",(gdouble)view->width,
									 "height",(gdouble)view->item_height,NULL);

	def->font_style=view->theme->font[THEME_FONT_SMALL];
	def->font_color_act=view->theme->color[THEME_COLOR_TEXT_ACTIVE];
	def->font_color_dis=view->theme->color[THEME_COLOR_TEXT_DISACTIVE];

	b=view->item_height/6;
    size=b*4;

	va_start(ap,   view); 
	while(s=va_arg(ap,const char*))
	{
		if(strcmp(s,SLMDEF_PRO_ICON)==0)
		{
			pixbuf=va_arg(ap,GdkPixbuf*);
			pixbuf=gdk_pixbuf_scale_simple(pixbuf,size,size,GDK_INTERP_BILINEAR);
			def->icon=goo_canvas_image_new (iter->cavas, pixbuf, (gdouble)b, (gdouble)b, NULL);
			//def->icon=goo_canvas_image_new (iter->cavas, pixbuf, 0, 0, NULL);
		}
		else if(strcmp(s,SLMDEF_PRO_TEXT)==0)
		{
			s=va_arg(ap,const char*);
			def->text=goo_canvas_text_new(iter->cavas,s,
									view->width-(view->width-view->item_height),
									view->item_height/2,-1,GTK_ANCHOR_WEST,
									"font",def->font_style,
									"fill_color",def->font_color_dis,
									NULL);
		}
		else
		{
			err=TRUE;
			break;
		}
		
	}
	va_end(ap); 
  
	if(err)
	{
		slmdef_destroy_iter(iter);
		return NULL;
	}

	return iter;
}

static gboolean slmdef_get_iter_property(SListIter *iter,const char *property,gpointer data)
{
	Slmdef *def;
	if(!iter||!property||!data||!def)
		return FALSE;
	
	def=iter->private;

	if(strcmp(property,SLMDEF_PRO_TEXT)==0)
	{
		g_object_get(G_OBJECT(def->text),property,data,NULL);
		return TRUE;
	}

	return FALSE;
}

static gboolean slmdef_set_iter_property(SListIter *iter,const char *property,gpointer data)
{
	Slmdef *def=iter->private;
	if(!iter||!property||!data||!def)
		return FALSE;

	if(strcmp(property,SLMDEF_PRO_TEXT)==0||strcmp(property,SLMDEF_PRO_ICON)==0)
	{
		g_object_set(G_OBJECT(def->text),property,data,NULL);
		return TRUE;
	}
	
	return FALSE;
}

static gboolean slmdef_highlight_iter(SListIter *iter,gboolean highlight)
{
	Slmdef *def=iter->private;
	if(iter->highlight==highlight||!def)
		return FALSE;
	
	iter->highlight=highlight;

	if(highlight)
		g_object_set(def->text,"fill_color",def->font_color_act,NULL);
	else
		g_object_set(def->text,"fill_color",def->font_color_dis,NULL);
}


SListMethod *slistview_default_method()
{
	SListMethod * method;
	method=(SListMethod *)g_malloc0(sizeof(SListMethod)+1);
	method->slistmethod_create_iter=slmdef_create_iter;
	method->slistmethod_destroy_iter=slmdef_destroy_iter;	
	method->slistmethod_get_iter_property=slmdef_get_iter_property;
	method->slistmethod_set_iter_property=slmdef_set_iter_property;	
	method->slistmethod_highlight_iter=slmdef_highlight_iter;
	
	return method;
}
